package gridmapper;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 *
 * @author estell
 */
public class MapBuilder {

	private int mapWidth = 0;
	private int mapHeight = 0;

	public int getMapHeight(Document doc, int id) {
		List nodeList = doc.selectNodes("//xml/map[@id='" + id + "']/quad/row");
		return nodeList.size();
	}

	public void setMapHeight(int mapHeight) {
		this.mapHeight = mapHeight;
	}

	public int getMapWidth(Document doc, int id) {
		List nodeList = doc.selectNodes("//xml/map[@id='" + id + "']/quad/row");

		Node tmp = (Node)nodeList.get(0);
		//System.out.println(tmp.getStringValue());
		String[] valuarry = tmp.getStringValue().split(",");
		return valuarry.length;
	}

	public void setMapWidth(int mapWidth) {
		this.mapWidth = mapWidth;
	}

	/**
	 *
	 * @param file
	 * @return
	 * @throws DocumentException
	 */
	public Document parse(File file) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(file);
		return document;
	}


	public String getAssignedMapResourceName(Document doc, int id){
		String retVal = "";
		retVal = doc.selectSingleNode("//xml/map[@id='" + id + "']/@imageResource").getText();
		System.out.println(retVal);
		return retVal;
	}
	/**
	 *
	 * @param nodeList
	 * @return
	 */
	public ArrayList<GridSpace[]> buildMap(Document doc, int id) {
		List nodeList = doc.selectNodes("//xml/map[@id='" + id + "']/quad/row");
		setMapHeight(nodeList.size());

		ArrayList<GridSpace[]> retVal = new ArrayList<GridSpace[]>();

		for (int i = 0; i < nodeList.size(); i++) {
			Element elem = (Element) nodeList.get(i);
			List<String> strings = Arrays.asList(elem.getStringValue().split(","));
			GridSpace[] spcArry = new GridSpace[strings.size()];

			for (int k = 0; k < strings.size(); k++) {
				GridSpace newSpace = new GridSpace();
				String compVal = strings.get(k);
				if (compVal.length() == 4){//@TODO add regex validation against the coordinate values to ensure proper character types
					for (Constants.Type type : Constants.Type.values()) {
						if (type.equals(compVal.substring(0, 1))) {
							newSpace.setType(type);
							newSpace.setSubtype(compVal.substring(1, 2));
							newSpace.setImageTileName(type.name() + compVal.substring(1, 2) + ".png");
						}
					}

					for (Constants.Orientation orientation : Constants.Orientation.values()) {
						if (orientation.equals(compVal.substring(2, 3))) {
							newSpace.setOrientation(orientation);
						}
					}

					for (Constants.Phase phase : Constants.Phase.values()) {
						if (phase.equals(compVal.substring(3))) {
							newSpace.setPhase(phase);
						}
					}
				}else{
					//the coordinate value is not valid length
					newSpace.setType(Constants.Type.TYPE_UNKNOWN);
					newSpace.setSubtype("a");
					newSpace.setImageTileName("unknown.png");
					newSpace.setOrientation(Constants.Orientation.UNKNOWN);
					newSpace.setPhase(Constants.Phase.BLOCKED);
				}

				newSpace.setXpos(k);
				newSpace.setYpos(i);
				spcArry[k] = newSpace;

				//retVal.add(newSpace);
			}

			retVal.add(spcArry);

		}
		return retVal;
	}
}
